package com.ccnu.rc.ftp;/*
 * @author raohj
 * @date 2019/11/2718:12
 * @description add your comment
 */

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class RFtpUtil {
    private String hostname;
    private int port;
    private String username;
    private String password;
    private String remotepath;
    private String localpath;
    private static final String dateReg = "2[0123456789][0123456789][0123456789][01][123456789][0123][0123456789]";

    public void downLoad(){
        FTPClient fc = new FTPClient();
        try {
            fc.connect(hostname, port);
            fc.login(username, password);
            fc.changeWorkingDirectory(remotepath);
            fc.setFileType(FTPClient.BINARY_FILE_TYPE);
            FTPFile[] ftpFilesRemote = fc.listDirectories();
            List<FTPFile> ftpFiles = new ArrayList<>();
            for (FTPFile ftpFile : ftpFilesRemote) {
                if(ftpFile.isDirectory() && ftpFile.getName().matches(dateReg)){
                    ftpFiles.add(ftpFile);
                }
            }
            if(ftpFiles.size()<=0){
                System.out.println("获取远程目录失败");
                return;
            }
            ftpFiles.sort(new Comparator<FTPFile>() {
                @Override
                public int compare(FTPFile ftpFile, FTPFile t1) {
                    return t1.getName().compareTo(ftpFile.getName());
                }
            });
            FTPFile ftpFile = ftpFiles.get(0);
            // 切换远程目录
            String realRemotePath = remotepath+ "/" + ftpFile.getName();
            System.out.println("切换到远程目录:" + realRemotePath);
            fc.changeWorkingDirectory(realRemotePath);
            FTPFile[] ftpFilesTBBS = fc.listFiles(realRemotePath);

            // 切换到本地目录
            String localPathYear = localpath + File.separator + ftpFile.getName();
            createLocalPath(localPathYear);
            // 获取远程目录文件
            for (FTPFile ftpFilesTBB : ftpFilesTBBS) {
                if(ftpFilesTBB.isFile()){
                    String localFile =  localPathYear + "/" + ftpFilesTBB.getName();
                    System.out.println("开始下载文件:" + localFile);
                    File fileExists = new File(localFile);
                    if(fileExists.exists()){
                        System.out.println("文件" + localFile + "本地已经存在，将会替换");
                    }
                    OutputStream os = new FileOutputStream(fileExists);
                    fc.retrieveFile(realRemotePath + "/" + ftpFilesTBB.getName(), os);
                    os.flush();
                    os.close();
                    System.out.println("下载文件:" + localFile + "成功");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fc.isConnected()){
                try {
                    fc.logout();
                    fc.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void createLocalPath(String createPath){
        File fileExists = new File(createPath);
        if(fileExists.exists()){
            return;
        }

        try {
            Path createDir = Paths.get(createPath);
            Files.createDirectory(createDir);		// 创建目录
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemotepath() {
        return remotepath;
    }

    public void setRemotepath(String remotepath) {
        this.remotepath = remotepath;
    }

    public String getLocalpath() {
        return localpath;
    }

    public void setLocalpath(String localpath) {
        this.localpath = localpath;
    }
}
